<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
    <link rel="stylesheet" href="./css/common_.css">
    <link rel="stylesheet" href="./css/reg.css">
    <script src="./js/jquery.min.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="./img/blog_logo.png" alt="">
    <p>思维驿站--燃笔博客</p>
    <a href="login.html" id="reg-login">登录</a>
</div>
<div class="container">
    <p>注册</p>
    <div class="login"><input type="file" id="user_photo" style="display: none">
        <img src="img/up_photo_logo.png" id="uped_photo" alt="上传头像"><span id="file-selected">上传头像</span>
    </div>
    <div class="login"><span>用户名</span><input type="text" id="username" placeholder="请输入用户名"></div>
    <div class="login"><span>昵称&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="nickname"
                                                                       placeholder="请输入昵称">
    </div>
    <div class="login"><span>密码&nbsp;&nbsp;&nbsp;&nbsp;</span>
        <input type="password" id="password" placeholder="请输入密码">
    </div>
    <div class="login"><span>确认密码</span>
        <input type="password" id="conword" placeholder="请输入确认密码">
    </div>
    <div class="login"><span>验证码</span>
        <input type="text" id="vr_code_pre" placeholder="请输入验证码">
        <img src="user/captcha" alt="验证码" id="vr_code" onclick="changeCode()">
    </div>


    <div class="btn">
        <button id="reg" type="submit" onclick="register()">注册</button>
    </div>
</div>
<script>
    function register() {
        var $username = jQuery('#username')
        var $password = jQuery('#password')
        var $conword = jQuery('#conword')
        var $vrCode = jQuery('#vr_code_pre')
        var $nickname = jQuery('#nickname')
        if ($username.val() == '') {
            alert('请输入用户名！')
            $username.focus()
            return
        }
        if ($nickname.val() == '') {
            alert('请输入昵称！')
            $nickname.focus()
            return
        }
        if ($password.val() == '') {
            alert('请输入密码！')
            $password.focus()
            return
        }
        if ($conword.val() == '') {
            alert('请输入确认密码！')
            $conword.focus()
            return
        }
        if ($vrCode.val() == '') {
            alert('请输入验证码！')
            $vrCode.focus()
            return
        }
        if ($password.val() != $conword.val()) {
            alert('密码和确认密码不一致！')
            $password.focus()
            return
        }
        jQuery.ajax({
            url: "user/register",
            type: "post",
            data: {
                "username": $username.val(),
                "password": $password.val(),
                "captcha": $vrCode.val(),
                "nickname": $nickname.val()
            },
            success: function (result) {
                if (result.code == 200) {
                    alert('注册成功！')
                    if ($('#user_photo').val() == '') {
                        location.href = "./login.html"
                    } else {
                        uploadPhoto()
                    }
                } else {
                    alert(result.msg)
                }
                changeCode()    //更新验证码
            }
        })
    }

    function changeCode() {
        jQuery('#vr_code').attr('src', 'user/captcha?' + Math.random())
    }

    function uploadPhoto() {
        var formData = new FormData();
        formData.append("updatePhoto", $("#user_photo")[0].files[0])
        jQuery.ajax({
            url: 'user/upd_photo',
            type: 'post',
            data: formData,
            contentType: false,
            processData: false,
            success: function (result) {
                if (result.code != 200) {
                    alert(result.msg)
                } else {
                    //头像上传成功
                    location.href = "./login.html"
                }
            }
        })
    }

    jQuery('.container .login #uped_photo').click(() => {
        jQuery('.container .login #user_photo').click()
    })

    jQuery('.container .login #user_photo').change(() => {

        if (jQuery('.container .login #user_photo').val() != '') {
            var val = jQuery('.container .login #user_photo').val();
            if(val.length > 8) {
                val = val.substring(0,8)+'...'
            }
            jQuery('.container .login #file-selected').text(val)
        }

    })
</script>
</body>

</html>